/**
 *
 * @param {number[]} asteroids
 * @returns {number[]}
 */
function asteroidCollision(asteroids) {
    let stackPlanet = extractSameDirectionPlanet(asteroids);

    console.log("stackPlanet", stackPlanet);

    return planetCollision(stackPlanet);
}

function planetCollision(stackPlanet) {
    let loopStart = true;
    const RIGHT = "right";
    const LEFT = "left";
    let resultArray = [];
    
    while (loopStart) {
        if (stackPlanet.length === 1) {
            resultArray.push(...stackPlanet[0]);
            loopStart = false;
            break;
        }

        if (stackPlanet.length === 0) {
            loopStart = false;
            break;
        }

        let onePlanetGroup = stackPlanet[0];
        let twoPlanetGroup = stackPlanet[1];
        let firstMoveDirection = onePlanetGroup[0] > 0 ? RIGHT : LEFT;
        let twoMoveDirection = twoPlanetGroup[0] > 0 ? RIGHT : LEFT;
        let controlIndexOne = 0;
        let controlIndexTwo = 0;

        if (firstMoveDirection === twoMoveDirection) {
            stackPlanet[0].push(...twoPlanetGroup);
            stackPlanet.splice(1, 1);
            continue;
        }

        if (firstMoveDirection === LEFT) {
            
            resultArray.push(...onePlanetGroup);
            
            stackPlanet.splice(0, 1);
            continue;
        }

        for (
            let lastIndex = onePlanetGroup.length - 1 - controlIndexOne;
            lastIndex >= 0;
            lastIndex--
        ) {
            let planetOne = onePlanetGroup[lastIndex];
            
            for (
                let firstIndex = controlIndexTwo;
                firstIndex < twoPlanetGroup.length;
                firstIndex++
            ) {
                let planetTwo = twoPlanetGroup[firstIndex];
                let collisionResult = Math.abs(planetOne) - Math.abs(planetTwo);
                
                if (collisionResult === 0) {
                    controlIndexOne++;
                    controlIndexTwo++;
                    break;
                }

                if (collisionResult > 0) {
                    controlIndexTwo++;
                    continue;
                }

                if (collisionResult < 0) {
                    controlIndexOne++;
                    break;
                }
            }

            // 两个都被撞碎了
            if (
                controlIndexOne === onePlanetGroup.length &&
                controlIndexTwo === twoPlanetGroup.length
            ) {
                stackPlanet.splice(0, 2);
                controlIndexOne = 0;
                controlIndexTwo = 0;
                break;
            }

            // 第一个行星组被撞碎了
            if (controlIndexOne === onePlanetGroup.length) {
                if (controlIndexTwo > 0) {
                    stackPlanet[1].splice(0, controlIndexTwo);
                }
                stackPlanet.splice(0, 1);
                controlIndexOne = 0;
                controlIndexTwo = 0;
                break;
            }

            // 第二个行星组被撞碎了
            if (controlIndexTwo === twoPlanetGroup.length) {
                if (controlIndexOne > 0) {
                    stackPlanet[0].splice(onePlanetGroup.length - controlIndexOne, controlIndexOne);
                }
                stackPlanet.splice(1, 1);
                controlIndexOne = 0;
                controlIndexTwo = 0;
                break;
            }
        }
    }

    return resultArray;
}

// 提取相同方向移动的行星
/**
 *
 * @param {*} asteroids
 * @returns {number[][]}
 */
function extractSameDirectionPlanet(asteroids) {
    let stackPlanet = [[]];

    asteroids.forEach((p) => {
        const lastArrayIndex = stackPlanet.length - 1;
        if (stackPlanet[lastArrayIndex] === undefined) {
            stackPlanet[lastArrayIndex] = [];
        }

        if (!stackPlanet[lastArrayIndex][0]) {
            return stackPlanet[lastArrayIndex].push(p);
        }

        if (stackPlanet[lastArrayIndex][0] > 0) {
            if (p > 0) {
                stackPlanet[lastArrayIndex].push(p);
            } else {
                stackPlanet.push([p]);
            }
        } else {
            if (p < 0) {
                stackPlanet[lastArrayIndex].push(p);
            } else {
                stackPlanet.push([p]);
            }
        }
    });

    return stackPlanet;
}

function getMaxIndex(numArray) {
    let resultIndex = 0;
    let max = numArray[0];
    numArray.forEach((p, index) => {
        if (index === numArray.length - 1) {
            return false;
        }
        if (p > max) {
            max = p;
            resultIndex = index;
        }
    });

    return resultIndex;
}

// console.log("result", asteroidCollision([5, 10, -5]));
console.log("result", asteroidCollision([-2,1,-1,-2]));